<template>
    <el-container class="home-container">
      <el-header>
        <div>
          <img src="../assets/heima.png">
          <span>电商后台管理系统</span>
        </div>
        <el-button type="info" @click="logout" class="logout">退出</el-button>
      </el-header>
      <el-container>
        <el-aside :width="isCollapse? '64px':'200px'">
          <div class="toggle-button" @click="toggleCollapse(isCollapse)">|||</div>
          <el-menu background-color="#333744"
                   text-color="#fff"
                   active-text-color="#409eff"
                   :default-active="activePath"
                   unique-opened
                   router
                   :collapse="isCollapse"
                   :collapse-transition="false">
          <el-submenu :index="item.id + ' '"
                      v-for="item in menuList"
                      :key="item.id">
            <template slot="title">
              <i :class="icons[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <el-menu-item :index="'/' + subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.id"
                          @click="_activePath('/' + subItem.path)">
              <i class="el-icon-menu"></i>
              <span>{{ subItem.authName }}</span>
            </el-menu-item>
          </el-submenu>
          </el-menu>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
</template>

<script>
export default {
  data () {
    return {
      menuList: [],
      icons: {
        125: 'iconfont icon-users',
        103: 'iconfont icon-tijikongjian',
        101: 'iconfont icon-shangpin',
        102: 'iconfont icon-danju',
        145: 'iconfont icon-baobiao'
      },
      activePath: '',
      isCollapse: false
    }
  },
  created () {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath') // 在页面创建之初就
  },
  methods: {
    logout () {
      window.sessionStorage.clear() // 清除本地存储中的token
      this.$router.push('/login') // 清除之后将网页重新跳转到登陆页面
    },
    async getMenuList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('获取用户列表失败！')
      this.menuList = res.data // 拿到菜单列表并在template中使用v-for渲染出列表
    },
    _activePath (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    },
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  color: white;
  font-size: 22px;
  > div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
}
.el-aside {
  background-color: #333744;
  font-size: 12px;
  .el-menu {
    border: 0;
    .el-submenu {
      span {
        margin-left: 10px;
        font-size: 12px;
      }
      .el-menu-item {
        span {
          margin: 0px;
        }
      }
    }
  }
}
.el-main {
  background-color: #eaedf1;
}
.toggle-button {
  background-color: #4A5064;
  font-size: 12px;
  line-height: 22px;
  color: #fff;
  text-align: center;
  letter-spacing: .12em;
  cursor: pointer;
}
</style>
